<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Rust Cool Nginx</title>
    <link rel="stylesheet" href="css/style.css">
    <style>
        .navbar {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            padding-top: 0.5rem;
            padding-bottom: 0.5rem;
            background: rgba(30, 30, 40, 0.7);
            box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15);
            border: 1px solid rgba(0, 255, 255, 0.2);
            border-radius: 10px;
            margin: 20px;
        }

        .navbar > .container {
            display: flex;
            flex-wrap: inherit;
            align-items: center;
            justify-content: space-between;
        }

        .navbar-brand {
            padding-top: 0.3125rem;
            padding-bottom: 0.3125rem;
            margin-right: 1rem;
            font-size: 1.25rem;
            text-decoration: none;
            white-space: nowrap;
            font-weight: 700;
            color: var(--primary);
            background: linear-gradient(90deg, var(--gradient-start), var(--gradient-end));
            -webkit-background-clip: text;
            background-clip: text;
            color: transparent;
        }

        .navbar-nav {
            display: flex;
            flex-direction: row;
            padding-left: 0;
            margin-bottom: 0;
            list-style: none;
        }

        .nav-link {
            display: block;
            padding: 0.5rem 1rem;
            color: #ccc;
            text-decoration: none;
            transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out, border-color 0.15s ease-in-out;
        }

        .nav-link:hover, .nav-link:focus {
            color: var(--primary);
        }

        .navbar-nav .nav-link.active {
            color: var(--primary);
            background: linear-gradient(90deg, var(--gradient-start), var(--gradient-end));
            -webkit-background-clip: text;
            background-clip: text;
            color: transparent;
        }

        .navbar-collapse {
            flex-basis: 100%;
            flex-grow: 1;
            align-items: center;
        }

        .navbar-toggler {
            padding: 0.25rem 0.75rem;
            font-size: 1.25rem;
            line-height: 1;
            background-color: transparent;
            border: 1px solid transparent;
            border-radius: 0.375rem;
            transition: box-shadow 0.15s ease-in-out;
        }

        .dashboard-header {
            text-align: center;
            padding: 30px 0;
            margin-bottom: 30px;
            background: rgba(30, 30, 40, 0.7);
            border: 1px solid rgba(0, 255, 255, 0.2);
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
        }
        
        .dashboard-header h1 {
            font-size: 2.5rem;
            background: linear-gradient(90deg, var(--gradient-start), var(--gradient-end));
            -webkit-background-clip: text;
            background-clip: text;
            color: transparent;
            margin-bottom: 10px;
            text-shadow: 0 0 20px rgba(0, 219, 222, 0.3);
            letter-spacing: 2px;
        }
        
        .status-card-container {
            display: flex;
            flex-wrap: wrap;
            gap: 20px;
            margin-bottom: 30px;
        }
        
        .status-card {
            background: rgba(30, 30, 40, 0.7);
            border: 1px solid rgba(0, 255, 255, 0.2);
            border-radius: 10px;
            padding: 20px;
            flex: 1;
            min-width: 200px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
            text-align: center;
        }
        
        .status-card-title {
            font-size: 1rem;
            color: #aaa;
            margin-bottom: 10px;
        }
        
        .status-card-value {
            font-size: 2rem;
            color: var(--primary);
            font-weight: bold;
        }
        
        .features-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 25px;
            margin: 40px 0;
        }
        
        .feature-card {
            background: rgba(30, 30, 40, 0.7);
            border: 1px solid rgba(0, 255, 255, 0.2);
            border-radius: 10px;
            padding: 25px;
            transition: all 0.3s ease;
            backdrop-filter: blur(10px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
        }
        
        .feature-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0, 255, 255, 0.2);
            border-color: rgba(0, 255, 255, 0.5);
        }
        
        .feature-card h3 {
            font-size: 1.5rem;
            margin-bottom: 15px;
            color: var(--primary);
        }
        
        .quick-links {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin: 30px 0;
        }
        
        .quick-link-card {
            background: rgba(30, 30, 40, 0.7);
            border: 1px solid rgba(0, 255, 255, 0.2);
            border-radius: 10px;
            padding: 20px;
            transition: all 0.3s ease;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
            text-decoration: none;
            color: var(--light);
        }
        
        .quick-link-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0, 255, 255, 0.2);
            border-color: rgba(0, 255, 255, 0.5);
        }
        
        .quick-link-card h3 {
            color: var(--primary);
            margin-bottom: 10px;
        }
        
        .quick-link-card p {
            color: #ccc;
            font-size: 0.9rem;
        }
    </style>
</head>
<body>
    <nav class="navbar">
        <div class="container">
            <a class="navbar-brand" href="index.html">Rust-Cool-Nginx</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span>Menu</span>
            </button>
            <div class="navbar-collapse" id="navbarNav">
                <ul class="navbar-nav ms-auto">
                    <li class="nav-item">
                        <a class="nav-link active" href="index.html">首页</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="config.html">配置</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="status.html">监控</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="static-file.html">静态文件</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>
    <div class="container">
        <div class="dashboard-header">
            <h1>Rust Cool Nginx</h1>
            <p>高性能 Web 服务器 | 基于 Rust 和 Tokio 构建</p>
        </div>
        
        <div class="status-card-container">
            <div class="status-card">
                <div class="status-card-title">服务器状态</div>
                <div class="status-card-value status-running" id="status-text">运行中</div>
            </div>
            <div class="status-card">
                <div class="status-card-title">版本</div>
                <div class="status-card-value">v0.1.0</div>
            </div>
            <div class="status-card">
                <div class="status-card-title">运行时间</div>
                <div class="status-card-value" id="uptime">0天</div>
            </div>
        </div>
        
        <div class="section-title">快速访问</div>
        <div class="quick-links">
            <a href="config.html" class="quick-link-card">
                <h3>服务器配置</h3>
                <p>查看服务器配置</p>
            </a>
            <a href="status.html" class="quick-link-card">
                <h3>监控面板</h3>
                <p>实时监控服务器状态</p>
            </a>
            <a href="static-file.html" class="quick-link-card">
                <h3>静态文件服务</h3>
                <p>管理静态文件服务器</p>
            </a>
            <a href="file-viewer.html?file=test/sample.txt" class="quick-link-card">
                <h3>测试文件</h3>
                <p>在文件查看器中查看测试文件</p>
            </a>
        </div>
        
        <div class="section-title">功能概览</div>
        <div class="features-grid">
            <div class="feature-card">
                <h3>基础功能</h3>
                <ul>
                    <li><span class="iconfont">&#10003;</span> 静态文件服务</li>
                    <li><span class="iconfont">&#10003;</span> 反向代理</li>
                    <li><span class="iconfont">&#10003;</span> 负载均衡</li>
                    <li><span class="iconfont">&#10003;</span> FastCGI 支持</li>
                </ul>
            </div>
            <div class="feature-card">
                <h3>安全特性</h3>
                <ul>
                    <li><span class="iconfont">&#10003;</span> 访问控制</li>
                    <li><span class="iconfont">&#10003;</span> 请求频率限制</li>
                    <li><span class="iconfont">&#10003;</span> SSL/TLS 加密</li>
                    <li><span class="iconfont">&#10003;</span> 防止注入攻击</li>
                </ul>
            </div>
            <div class="feature-card">
                <h3>性能优化</h3>
                <ul>
                    <li><span class="iconfont">&#10003;</span> Gzip 压缩</li>
                    <li><span class="iconfont">&#10003;</span> 缓存机制</li>
                    <li><span class="iconfont">&#10003;</span> WebSocket 支持</li>
                    <li><span class="iconfont">&#10003;</span> 高并发处理</li>
                </ul>
            </div>
        </div>
    </div>
    
    <script>
        // 检查是否在 Tauri 环境中运行
        const isTauri = !!window.__TAURI__;
        
        // 获取基础URL
        function getBaseURL() {
            if (window.location.origin === 'http://tauri.localhost') {
                return 'http://127.0.0.1';
            } else {
                return window.location.origin;
            }
        }
        
        // 获取服务器配置信息
        async function fetchServerConfig() {
            try {
                let config;
                if (isTauri && window.__TAURI__ && window.__TAURI__.core) {
                    // 在 Tauri 环境中，通过 Tauri 命令获取配置
                    console.log('在Tauri环境中，尝试调用Tauri命令获取配置');
                    try {
                        config = await window.__TAURI__.core.invoke('get_config');
                        console.log('通过Tauri获取到的配置:', config);
                    } catch (tauriError) {
                        console.error('Tauri调用失败:', tauriError);
                        throw tauriError;
                    }
                } else {
                    // 在 Web 环境中，通过 HTTP API 获取配置
                    console.log('在Web环境中，通过HTTP API获取配置');
                    const apiUrl = `${getBaseURL()}/api/config`;
                    console.log('API URL:', apiUrl);
                    
                    const response = await fetch(apiUrl);
                    const contentType = response.headers.get('content-type');
                    
                    if (contentType && contentType.includes('application/json')) {
                        // 检查响应是否为 JSON 格式
                        if (!response.ok) {
                            throw new Error(`HTTP error! status: ${response.status}`);
                        }
                        config = await response.json();
                        console.log('通过HTTP API获取到的配置:', config);
                    } else {
                        // 如果响应不是 JSON 格式，抛出错误
                        const text = await response.text();
                        console.log('HTTP API 响应内容:', text);
                        console.error('HTTP API 返回非 JSON 响应:', text.substring(0, 100) + '...');
                        throw new Error('HTTP API 返回非 JSON 响应');
                    }
                }
                return config;
            } catch (error) {
                console.error('获取配置信息失败:', error);
                // 如果获取配置失败，返回默认配置
                return {
                    server: {},
                    upstream: {},
                    features: {
                        static_file_serving: true,
                        reverse_proxy: true,
                        load_balancing: true,
                        fastcgi_support: false,
                        cache_enabled: false,
                        gzip_compression: true,
                        access_control: true,
                        rate_limiting: true,
                        ssl_tls: false,
                        websocket_support: true
                    }
                };
            }
        }
        
        // 获取监控数据
        async function fetchMonitoringData() {
            try {
                let monitoringData;
                const apiUrl = `${getBaseURL()}/nginx_status`;
                console.log('监控数据 API URL:', apiUrl);
                
                const response = await fetch(apiUrl);
                const contentType = response.headers.get('content-type');
                
                if (contentType && contentType.includes('application/json')) {
                    if (!response.ok) {
                        throw new Error(`HTTP error! status: ${response.status}`);
                    }
                    monitoringData = await response.json();
                    console.log('通过API获取到的监控数据:', monitoringData);
                } else {
                    const text = await response.text();
                    console.log('监控数据 API 响应内容:', text);
                    throw new Error('监控数据 API 返回非 JSON 响应');
                }
                return monitoringData;
            } catch (error) {
                console.error('获取监控数据失败:', error);
                // 返回默认监控数据
                return {
                    total_requests: 0,
                    current_connections: 0,
                    success_rate: 0,
                    uptime: "0天"
                };
            }
        }
        
        // 更新首页显示信息
        async function updateHomePage() {
            try {
                // 获取服务器配置
                const config = await fetchServerConfig();
                
                // 获取监控数据
                const monitoringData = await fetchMonitoringData();
                
                // 更新服务器状态
                document.getElementById('status-text').textContent = '运行中';
                document.getElementById('status-text').classList.add('status-running');
                document.getElementById('status-text').classList.remove('status-stopped');
                
                // 更新运行时间（使用真实数据）
                document.getElementById('uptime').textContent = monitoringData.uptime || '0天';
                
                // 更新功能状态显示
                updateFeatureStatus(config);
            } catch (error) {
                console.error('更新首页信息失败:', error);
                document.getElementById('status-text').textContent = '已停止';
                document.getElementById('status-text').classList.remove('status-running');
                document.getElementById('status-text').classList.add('status-stopped');
            }
        }
        
        // 根据配置更新功能状态显示
        function updateFeatureStatus(config) {
            const featureCards = document.querySelectorAll('.feature-card ul');
            
            // 更新基础功能状态
            if (featureCards[0]) {
                const items = featureCards[0].querySelectorAll('li');
                if (items[0]) { // 静态文件服务
                    const icon = items[0].querySelector('.iconfont');
                    if (icon) {
                        icon.textContent = config.features.static_file_serving ? '\u2713' : '\u2717'; // ✓ or ✗
                        icon.style.color = config.features.static_file_serving ? 'green' : 'red';
                    }
                }
                if (items[1]) { // 反向代理
                    const icon = items[1].querySelector('.iconfont');
                    if (icon) {
                        icon.textContent = config.features.reverse_proxy ? '\u2713' : '\u2717';
                        icon.style.color = config.features.reverse_proxy ? 'green' : 'red';
                    }
                }
                if (items[2]) { // 负载均衡
                    const icon = items[2].querySelector('.iconfont');
                    if (icon) {
                        icon.textContent = config.features.load_balancing ? '\u2713' : '\u2717';
                        icon.style.color = config.features.load_balancing ? 'green' : 'red';
                    }
                }
                if (items[3]) { // FastCGI 支持
                    const icon = items[3].querySelector('.iconfont');
                    if (icon) {
                        icon.textContent = config.features.fastcgi_support ? '\u2713' : '\u2717';
                        icon.style.color = config.features.fastcgi_support ? 'green' : 'red';
                    }
                }
            }
            
            // 更新安全特性状态
            if (featureCards[1]) {
                const items = featureCards[1].querySelectorAll('li');
                if (items[0]) { // 访问控制
                    const icon = items[0].querySelector('.iconfont');
                    if (icon) {
                        icon.textContent = config.features.access_control ? '\u2713' : '\u2717';
                        icon.style.color = config.features.access_control ? 'green' : 'red';
                    }
                }
                if (items[1]) { // 请求频率限制
                    const icon = items[1].querySelector('.iconfont');
                    if (icon) {
                        icon.textContent = config.features.rate_limiting ? '\u2713' : '\u2717';
                        icon.style.color = config.features.rate_limiting ? 'green' : 'red';
                    }
                }
                if (items[2]) { // SSL/TLS 加密
                    const icon = items[2].querySelector('.iconfont');
                    if (icon) {
                        icon.textContent = config.features.ssl_tls ? '\u2713' : '\u2717';
                        icon.style.color = config.features.ssl_tls ? 'green' : 'red';
                    }
                }
                if (items[3]) { // 防止注入攻击
                    const icon = items[3].querySelector('.iconfont');
                    if (icon) {
                        // 假设这个功能总是启用的
                        icon.textContent = '\u2713';
                        icon.style.color = 'green';
                    }
                }
            }
            
            // 更新性能优化状态
            if (featureCards[2]) {
                const items = featureCards[2].querySelectorAll('li');
                if (items[0]) { // Gzip 压缩
                    const icon = items[0].querySelector('.iconfont');
                    if (icon) {
                        icon.textContent = config.features.gzip_compression ? '\u2713' : '\u2717';
                        icon.style.color = config.features.gzip_compression ? 'green' : 'red';
                    }
                }
                if (items[1]) { // 缓存机制
                    const icon = items[1].querySelector('.iconfont');
                    if (icon) {
                        icon.textContent = config.features.cache_enabled ? '\u2713' : '\u2717';
                        icon.style.color = config.features.cache_enabled ? 'green' : 'red';
                    }
                }
                if (items[2]) { // WebSocket 支持
                    const icon = items[2].querySelector('.iconfont');
                    if (icon) {
                        icon.textContent = config.features.websocket_support ? '\u2713' : '\u2717';
                        icon.style.color = config.features.websocket_support ? 'green' : 'red';
                    }
                }
                if (items[3]) { // 高并发处理
                    const icon = items[3].querySelector('.iconfont');
                    if (icon) {
                        // 假设这个功能总是启用的
                        icon.textContent = '\u2713';
                        icon.style.color = 'green';
                    }
                }
            }
        }
        
        // 页面加载后检查服务器状态
        window.addEventListener('DOMContentLoaded', () => {
            updateHomePage();
            
            // 每5秒更新一次数据
            setInterval(updateHomePage, 5000);
        });
    </script>
</body>
</html>